define(['./BaseView', '../libs/extend', 'underscore'], function (BaseView, extend, _) {
    return BaseView.extend({
        searchList: null,
        pagenum: null,
        sarchData: null,
        events: {
            "click #searchData": "searchData",
            "focus #searchFunction": "searchFunction",
            "keydown #searchFunction": "searchInputKeydown",
            "blur #searchFunction": "hideSearchUl",
            "click #cancelSer": "cancelSer",
            "click .clearHistory": "clearHistory",
            "click .searchLi": "searchLi",
            "click .list-item": "listItem"
        },
        //初始化
        initialize: function () {
            pagenum = 2;
            if (extend.getlocalStorage('searchHistory')) {
                var history = extend.getlocalStorage('searchHistory');
                searchList = history.split(',');
            } else {
                searchList = [];
            }
            var sear = library.getTemplate('searchPage.html');
            this.$el.append(sear);
            var template = library.getTemplate('loading.html');
            this.$el.append(template);
            this.$el.find('#searchUl').html('');
            this.$el.find('#searchFunction').val('');
            this.$el.find('.searchDataList').hide();
            var searchHistory = extend.getlocalStorage('searchHistory');
            if (searchHistory) {
                var searchHistoryArr = searchHistory.split(',');
                for (var i = 0; i < searchHistoryArr.length; i++) {
                    var searchLi = '<li class="searchLi">' + searchHistoryArr[i] + '</li>';
                    this.$el.find('#searchUl').append(searchLi)
                }
                this.$el.find('.clearHistory').show();
            }
        },
        touchListener: function () {
            var that = this;
            $('.searchDataList').bind("touchcancel touchend", function () {
                var height = $(this).height();
                var scrolltop = $(this).scrollTop();
                var offsetH = $('.toSearchData').outerHeight();
                that.scroll();
                if (scrolltop >= offsetH - height - 50 && scrolltop != 0) {
                    $('.searchDataList').unbind('scroll');
                    $('.pullup').hide();
                    that.$el.find('.loading').show();
                    var indexData = {
                        "pageindex": pagenum,
                        "pagesize": 10,
                        "querystr": sarchData
                    };
                    extend.sendData(window.app.proxy.root + 'allResearchRecords', indexData, function (res) {
                        if (res.status == 'success') {
                            $('.searchBk').hide();
                            $('.searchDataList').show();
                            if (res.data.length == 0) {
                                that.$el.find('.loading').hide();
                                that.$el.find('.noMore').show();
                                setTimeout(
                                    function () {
                                        that.$el.find('.noMore').hide();
                                    }, 300
                                );
                                return false
                            } else {
                                setTimeout(function () {
                                    that.createList(res.data);
                                    pagenum++;
                                }, 300)
                            }
                        } else {
                            extend.Toast(res.message, 2000);
                            return false;
                        }
                    }, function (e) {
                        e.Toast('网络错误', 2000);
                    });
                }
            });
        },
        //监听页面滚动到底部
        scroll: function () {
            $('.searchDataList').bind("scroll", function () {
                var height = $(this).height();
                var scrolltop = $(this).scrollTop();
                var offsetH = $('.toSearchData').outerHeight();
                if (scrolltop >= offsetH - height) {
                    $('.pullup').show();
                } else {
                    $('.pullup').hide();
                }
            });
        },
        //点击键盘回车键进行查询
        searchInputKeydown: function (e) {
            var keyword = this.$el.find('#searchFunction').val();
            if (e.keyCode == 13) {
                //确认
                if (keyword) {
                    //跳转关键字
                    this.$el.find('#searchFunction').blur();
                    this.searchData();
                }
            }
        },
        //点击搜索进行查询
        searchData: function () {
            var that = this;
            pagenum = 2;
            this.$el.find('.toSearchData').html('');
            $('#cancelSer').show();
            $('#searchData').hide();
            $('.searchDataList').show();
            var searchVal = $('#searchFunction').val();
            var arr = searchVal.split('');
            var kong = [];
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] != ' ') {
                    kong.push(arr[i])
                }
            }
            var serdata = kong.join('');
            if (serdata != '') {     //存入历史记录
                that.sendData(serdata);
                for (var k = 0; k < searchList.length; k++) {
                    if (serdata == searchList[k]) {
                        return false
                    }
                }
                searchList.unshift(serdata);
                if (searchList.length > 5) {
                    searchList.pop();
                }
                extend.setlocalStorage('searchHistory', searchList.join());
            } else {
                extend.Toast('请输项目、人员、部门查找', 2000);
            }
        },
        //发送请求
        sendData: function (data) {
            var that = this;
            $('.searchDataList').unbind();
            sarchData = data;
            var indexData = {
                "pageindex": 1,
                "pagesize": 10,
                "querystr": data
            };
            extend.sendData(window.app.proxy.root + 'allResearchRecords', indexData, function (res) {
                if (res.status == 'success') {
                    $('.searchBk').hide();
                    $('.searchDataList').show();
                    that.createList(res.data);
                    that.touchListener();
                } else {
                    extend.Toast(res.message, 2000);
                }
            }, function (e) {
                extend.Toast('网络错误', 2000);
            });
        },
        //渲染列表
        createList: function (data) {
            var len = data.length;
            if (len) {
                this.$el.find('.toSearchData').show();
                for (var i = 0; i < len; i++) {
                    var item = data[i];
                    var itemId = item.pk_record;
                    var Username = item.username; //人員姓名
                    var project = item.project; //項目
                    var begindate = item.begindate;//起始時間
                    if (item) {
                        var listItemDom = [
                                '<li class="list-item" data-id=' + itemId + '>',
                                '<p class="project">' + project + '</p>',
                                '<p class="username"><span>负责人：</span><span>' + Username + '</span></p>',
                                '<span class="begindate">' + begindate + '</span>',
                                '</li>'
                            ].join(''),
                            $listItemDom = $(listItemDom);
                        $('.toSearchData').append($listItemDom);
                    }
                }
                this.$el.find('.loading').hide();

            } else {
                extend.Toast('没有查到相关记录', 2000);
                this.$el.find('.toSearchData').html('').hide();
            }
        },

        //获取搜索框焦点，搜索历史记录
        searchFunction: function () {
            $('#searchFunction').val('');
            $('#searchData').show();
            $('#cancelSer').hide();
            $('.searchDataList').hide();
            $('.searchBk').show();
            $('#searchUl').html('');
            var searchHistory = extend.getlocalStorage('searchHistory');
            if (searchHistory) {
                var searchHistoryArr = searchHistory.split(',');
                for (var i = 0; i < searchHistoryArr.length; i++) {
                    var searchLi = '<li class="searchLi">' + searchHistoryArr[i] + '</li>';
                    $('#searchUl').append(searchLi)
                }
                $('.clearHistory').show();
            }
        },
        hideSearchUl: function () {
            $('#searchData').hide();
            $('#cancelSer').show();
        },

        //点击搜索历史实现搜索功能
        searchLi: function (e) {
            e.preventDefault();
            pagenum = 2;
            this.$el.find('.toSearchData').html('');
            var serHisHtml = $(e.currentTarget).html();
            $('#searchFunction').val(serHisHtml);
            this.sendData(serHisHtml)
        },
        //返回首页
        cancelSer: function () {
            Backbone.history.navigate('#q48JG4RVQPageView', {trigger: true});
        },
        //清空搜索歷史記錄
        clearHistory: function () {
            $('#searchUl').html('');
            $('.clearHistory').hide();
            extend.setlocalStorage('searchHistory', '');
        },
        //点击列表进入详情页
        listItem: function (e) {
            var dataid = e.currentTarget.getAttribute("data-id");
            //存在本地数据库
            extend.setlocalStorage('YY_themsID', dataid);
            Backbone.history.navigate('#Particular', {
                trigger: true
            });
        }
    });
});
